package com.baozun.houji.me.fragment

import android.os.Bundle
import android.os.CountDownTimer
import android.view.View
import com.baozhun.mall.common.base.BaseFragment
import com.baozun.houji.me.R
import com.baozun.houji.me.activity.account.ForgetPwdActivity
import com.baozun.houji.me.activity.account.RegisterActivity
import com.baozun.houji.me.databinding.FragmentPhoneLoginBinding
import com.baozun.houji.me.viewmodel.LoginViewModel

/**
 * @Title：手机号登陆
 * @Description：
 * @Author：xuefeng.zhu
 * @Since：2022/11/4
 * @Version：1.0.0
 */
class PhoneLoginFragment : BaseFragment<LoginViewModel, FragmentPhoneLoginBinding>() {
    var timer: CountDownTimer? = null

    companion object {
        fun newInstance(): PhoneLoginFragment {
            return PhoneLoginFragment()
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v?.id) {
            R.id.tv_get_verify_code -> {
                mViewModel.getVerifyCode {
                    startCountDownTimer()
                }
            }

            R.id.tv_register_hint -> { //注册
                RegisterActivity.start(mActivity)
            }

            R.id.tv_forget_pwd -> { //忘记密码
                ForgetPwdActivity.start(mActivity)
            }

            R.id.tv_login -> mViewModel.login() {
                mActivity?.finish()
            }
        }
    }

    /**
     * 开启倒计时
     */
    private fun startCountDownTimer() {
        timer?.cancel()
        timer = object : CountDownTimer(60 * 1000L, 1000L) {
            override fun onTick(millisUntilFinished: Long) {
                mViewModel.remainingTime.postValue(millisUntilFinished.toInt() / 1000)
            }

            override fun onFinish() {
                mViewModel.remainingTime.postValue(0)
            }

        }
        timer?.start()
    }

    override fun onDestroy() {
        super.onDestroy()
        timer?.cancel()
    }

    override fun createObserver() {

    }
}